from eden.evo.context import Context
import doctest

class Operand:
    pass

class ConstOperand(Operand):
    INSTANCES = None
    
    def __init__(self, value):
        self.__value = value
        
    @staticmethod
    def getinstance(value):
        return ConstOperand.INSTANCES[value]
        
    def getvalue(self, context):
        '''
        >>> ConstOperand(1).getvalue(None)
        1
        >>> ConstOperand(2).getvalue(None)
        2
        '''
        return self.__value
    
class RegisterOperand(Operand):
    INSTANCES = None
    
    def __init__(self, index):
        self.__index = index
        
    @staticmethod
    def getinstance(index):
        return RegisterOperand.INSTANCES[index]
        
    def getindex(self):
        return self.__index
        
    def getvalue(self, context):
        '''
        >>> from eden.evo.context import Context
        >>> context = Context()
        >>> context.setregister(0, 123)
        >>> RegisterOperand(0).getvalue(context)
        123
        '''
        return context.getregister(self.__index)
    
    def setvalue(self, value, context):
        '''
        >>> from eden.evo.context import Context
        >>> context = Context()
        >>> operand = RegisterOperand(1)
        >>> operand.setvalue(123, context)
        >>> context.getregister(1)
        123
        '''
        context.setregister(self.__index, value)

ConstOperand.INSTANCES = [ConstOperand(i) for i in range(128)]
RegisterOperand.INSTANCES = [RegisterOperand(i) for i in range(Context.REGISTER_COUNT)]

if __name__ == '__main__':
    doctest.testmod()